//
//  SEChatViewController+ActionBar.swift
//  SmartEyeForPatient
//
//  Created by QDSG on 2021/4/8.
//

import UIKit

// MARK: - SEChatActionBarViewDelegate

extension SEChatViewController: SEChatActionBarViewDelegate {
    func chatActionBarRecordVoiceHideKeyboard() {
        hideCustomKeyboard()
    }
    
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedMoreButton button: SEChatButton) {
        actionBar.resetAllButtonUI()
        if button.showTextKeyboard {
            actionBar.showTextKeyboard()
        } else {
            actionBar.showMoreKeyboard()
        }
        controlExpandableInputView(showExpandable: true)
    }
    
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedVoiceButton button: SEChatButton) {
        actionBar.resetAllButtonUI()
        let showRecording = actionBar.recordButton.isHidden
        if showRecording {
            actionBar.showRecording()
            button.replaceVoiceButtonUI(showKeyboard: true)
            controlExpandableInputView(showExpandable: false)
        } else {
            actionBar.showTextKeyboard()
            button.replaceVoiceButtonUI(showKeyboard: false)
            controlExpandableInputView(showExpandable: true)
        }
    }
    
    func chatActionBar(_ actionBar: SEChatActionBarView, didClickedSendButton button: UIButton) {
        checkSendTextEnable()
    }
    
    func chatActionBarShowMoreKeyboard() {
        let heightOffset = chatMoreView.height
        msgTableView.stopScrolling()
        actionBarPaddingBottomConstranit?.update(offset: -heightOffset)
        
        view.bringSubviewToFront(chatMoreView)
        chatMoreView.snp.updateConstraints { [weak self] (make) in
            guard let self = self else { return }
            make.top.equalTo(self.chatActionBarView.snp.bottom)
        }
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear) {
            self.view.layoutIfNeeded()
            self.msgTableView.scrollBottomToLastRow()
        }
    }
    
    func chatActionBar(_ actionBar: SEChatActionBarView, didLongTapGesture gesture: UILongPressGestureRecognizer) {
        switch gesture.state {
        case .began:
            finishRecording = true
            voiceIndicatorView.recording()
            SEVoiceRecordManager.shared.startRecord()
            actionBar.recordButton.replaceRecordButtonUI(isRecording: true)
        case .changed:
            let point = gesture.location(in: voiceIndicatorView)
            if voiceIndicatorView.point(inside: point, with: nil) {
                voiceIndicatorView.slideToCancelRecord()
                finishRecording = false
            } else {
                voiceIndicatorView.recording()
                finishRecording = true
            }
        case .ended:
            if finishRecording {
                SEVoiceRecordManager.shared.stopRecord()
            } else {
                SEVoiceRecordManager.shared.cancelRecord()
            }
            voiceIndicatorView.endRecord()
            actionBar.recordButton.replaceRecordButtonUI(isRecording: false)
        default:
            break
        }
    }
    
    // MARK: -
    
    /// 控制actionBarView的高度：
    /// 当用户想要显示录制键盘时，应将actionBarView的高度设置为原始值。
    /// 否则我们应该将actionBarView的高度设置为currentHeight
    func controlExpandableInputView(showExpandable: Bool) {
        let textView = chatActionBarView.inputTextView
        let currentTextHeight = chatActionBarView.inputTextViewCurrentHeight
        
        let textHeight = showExpandable ? currentTextHeight : kChatActionBarOriginalHeight
        self.chatActionBarView.snp.updateConstraints { (make) in
            make.height.equalTo(textHeight)
        }
        
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear) {
            self.view.layoutIfNeeded()
            self.msgTableView.scrollBottomToLastRow()
            textView?.contentOffset = .zero
        }
    }
}
